const AIService = require('./');
const TimeHepler = require('./TimeHepler');
var AnalyzeStatus = require('./model/AIAnalyzeData').AnalyzeStatus;
var AIAnalyzeData = require('./model/AIAnalyzeData').AIAnalyzeData;
const assert  = require("assert");
function getAnalyzes() {
    let analyzes = new Map();
    for (let i = 0; i < 10; i = i + 1) {
        let data = new AIAnalyzeData();
        data.cameraId = "40000" + i;
        data.extends = "40000140000" + i;
        data.cameraIP = "10.18.227.96";
        data.fps = 10;
        data.remark = "this is a camera";
        data.type = "high";
        data.uri = "/opt/high-3m.mp4";
        analyzes.set(data.cameraId, data);
    }
    return analyzes;
}

function createProxy() {
    let analyzes = getAnalyzes();
    let proxy = new AIService("https://10.18.226.126:8888", "LBA9tBUAogQJfnZSAf");
    proxy.setAnalyzes(analyzes);
    return proxy;
}
async function startAnalyzeTest() {
    let data = new AIAnalyzeData();
    data.cameraId = "400001";
    data.extends = "400001400001";
    data.cameraIP = "10.18.227.96";
    data.fps = 10;
    data.remark = "this is a camera";
    data.type = "high";
    data.uri = "/opt/high-3m.mp4";
    let proxy = new AIService("https://10.18.226.126:8888", "LBA9tBUAogQJfnZSAf");
    proxy.startAnalyze(data);
    await TimeHepler.sleep(10000);
    proxy.stopAnalyze(data);
    console.error("---------");
    await TimeHepler.sleep(10000);
}

async function startAnalyzesAnyTest() {
    let proxy = createProxy();
    proxy.startAnalyzes();
    console.error("---------");
    await TimeHepler.sleep(10000);

    analyzes.forEach((analyze, id) => {
        assert.equal(analyze.status, AnalyzeStatus.Started);
    });
    proxy.stopAnalyzes(analyzes);
    await TimeHepler.sleep(10000);
    analyzes.forEach((analyze, id) => {
        assert.equal(analyze.status, AnalyzeStatus.ReleaseRoom);
    });
}
async function startAnalyzeByIdTest() {
    let proxy = createProxy();
    let cameraId = "400005";
    proxy.startAnalyzeById(cameraId);
    await TimeHepler.sleep(10000);
    let analyze = proxy.getAnalyzeById(cameraId);
    assert.notEqual(null, analyze);
    assert.equal(analyze.status, AnalyzeStatus.Started);
    proxy.stopAnalyzeById(cameraId);
    await TimeHepler.sleep(10000);analyze = proxy.getAnalyzeById(cameraId);
    assert.notEqual(null, analyze);
    assert.equal(analyze.status, AnalyzeStatus.ReleaseRoom);

}

async function getAnalyzesTest() {
    let proxy = createProxy();
    let rets = proxy.getAnalyzes();
    assert.notEqual(null, rets);
    for (var i = 0; i < rets.length; i++) {
        console.error((i + 1) + ":" + JSON.stringify(rets[i]));
    }
}

async function getAnalyzeByIdTest() {
    let proxy = createProxy();
    let cameraId = "400005";
    let analyze = proxy.getAnalyzeById(cameraId);
    assert.notEqual(null, analyze);
    console.error(JSON.stringify(analyze));

}

//startAnalyzeTest();
startAnalyzesAnyTest();
//getAnalyzesTest();
//getAnalyzeByIdTest();
//startAnalyzeByIdTest();
